package com.shisfish.library.Controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Impl.UserLevelImpl;
import com.shisfish.library.Impl.UserSingleLendImpl;
import com.shisfish.library.Model.User;
import com.shisfish.library.Model.UserLevel;
import com.shisfish.library.Model.UserSingleLend;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("userLevel")
public class UserLevelController extends GlobalVariable {
	
	private UserLevelImpl userLevelImpl = new UserLevelImpl();
	private UserImpl userImpl = new UserImpl();
	private UserSingleLendImpl userSingleLendImpl = new UserSingleLendImpl();
	
	@POST
	@Path("insertALevel/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertALevel(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("borrow_numbers") int borrow_numbers, @FormParam("borrow_days") int borrow_days, 
			@FormParam("loan_numbers") int loan_numbers, @FormParam("loan_borrow_days") int loan_borrow_days, 
			@FormParam("arrearage") BigDecimal arrearage, @Context HttpServletRequest request, 
			@FormParam("moreday") int moreday) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			UserLevel userLevel = new UserLevel(name, borrow_numbers, borrow_days, loan_numbers, loan_borrow_days, arrearage, moreday);
			
			int flag = userLevelImpl.insertOne(userLevel);
			
			if (flag == 1) {
				globalMap.put("message", ADD_SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateALevel/{idadmin}/{code}")
	@Produces("application/json; charset=utf-8")
	public String updateALevel(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("borrow_numbers") int borrow_numbers, @FormParam("borrow_days") int borrow_days, 
			@FormParam("loan_numbers") int loan_numbers, @FormParam("loan_borrow_days") int loan_borrow_days, 
			@FormParam("arrearage") BigDecimal arrearage, @Context HttpServletRequest request, 
			@PathParam("code") int code, @FormParam("moreday") int moreday) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			// 把全部该权限下的用户信息修改掉
			
			List<User> users = userImpl.selectByLevel(code);
			
			for (User user : users) {
				UserSingleLend userSingleLend = user.getUserSingleLend();
				userSingleLend.setBorrow_number(borrow_numbers);
				userSingleLend.setLoaned_number(loan_numbers);
				userSingleLendImpl.updateOne(userSingleLend);
			}
			
			UserLevel userLevel = userLevelImpl.selectByCode(code);
			
			userLevel.setArrearage(arrearage);
			userLevel.setBorrow_days(loan_borrow_days);
			userLevel.setBorrow_numbers(borrow_numbers);
			userLevel.setLoan_borrow_days(loan_borrow_days);
			userLevel.setLoan_numbers(loan_numbers);
			userLevel.setName(name);
			userLevel.setMoreday(moreday);
			
			int flag = userLevelImpl.updateOne(userLevel);
			
			if (flag == 1) {
				globalMap.put("message", ADD_SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAllPage/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = userLevelImpl.countAll();
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<UserLevel> userLevels = userLevelImpl.selectAllPage(page.getBeginIndex(), pageSize);
			if (userLevels.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", userLevels);
			} else if (userLevels.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
